package com.ilvyou.api.controller;

import com.ilvyou.api.common.Cache;
import com.ilvyou.api.common.Constant;
import com.ilvyou.api.bean.result.Result;
import com.ilvyou.core.util.Util;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by GuanYuCai on 2016/9/7 0007.
 */
public class BaseController {

    protected Long getUserId(String token){
        return Cache.get(token).actId;
    }

    protected Result success(Object obj){
        Result result = new Result();
        result.setCode(Constant.OK);
        result.setMessage("");
        result.setResult(obj);
        return result;
    }

    protected Result success(Object obj, Object fields){
        Map<String, Object> map = new HashMap<String, Object>();
        if (obj instanceof String){
            map.put((String) obj, fields);
        }else{
            String[] strs = ((String) fields).split(",");

            for (String str : strs){
                str = str.trim();
                map.put(str, Util.getter(str, obj));
            }
        }

        return success(map);
    }

    protected Result fail(String msg){
        Result result = new Result();
        result.setCode(Constant.ERR);
        result.setMessage(msg);
        return result;
    }

    protected Result needLogin(){
        Result result = new Result();
        result.setCode(Constant.LOGIN);
        result.setMessage("请登录！");
        return result;
    }
}
